package com.crowd.service.impl;

import com.crowd.entity.Role;
import com.crowd.entity.RoleExample;
import com.crowd.mapper.RoleMapper;
import com.crowd.service.api.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Stone
 * @date 2020/5/18 14:56
 */
@Service
@Transactional
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

   /**
    * @Author:Stone
      * @Param pageNum:
    * @Param pageSize:
    * @Param keyword:
      * @return: com.github.pagehelper.PageInfo<com.crowd.entity.Role>
    * @Date: 2020/5/18 15:27
    * @Description:分页方式，通过keyword查找数据
   */
    @Override
    public PageInfo<Role> getPageInfo(Integer pageNum, Integer pageSize, String keyword) {
        // 1.开启分页
        PageHelper.startPage(pageNum, pageSize);
        // 2.查找数据
        List<Role> roleList = roleMapper.selectRoleByKeyword(keyword);
        // 3.通过PageInfo封装数据
        PageInfo<Role> pageInfo = new PageInfo<>(roleList);
        return pageInfo;
    }

    /**
     * @Author:Stone
       * @Param role:
       * @return: void
     * @Date: 2020/5/19 15:50
     * @Description:保存role
    */
    @Override
    public void saveRole(Role role) {
        roleMapper.insert(role);
    }

    /**
     * @Author:Stone
       * @Param role:
       * @return: void
     * @Date: 2020/5/19 17:59
     * @Description:更新role
    */
    @Override
    public void updateRole(Role role) {
        roleMapper.updateByPrimaryKey(role);
    }

    /**
     * @Author:Stone
       * @Param roleIdList:
       * @return: void
     * @Date: 2020/5/20 14:38
     * @Description:删除role，此处可做单条删除，也可做批量删除
    */
    @Override
    public void removeRole(List<Integer> roleIdList) {
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        criteria.andIdIn(roleIdList);
        roleMapper.deleteByExample(roleExample);
    }

    /**
     * @Author:Stone
       * @Param adminId:
       * @return: java.util.List<com.crowd.entity.Role>
     * @Date: 2020/5/25 14:17
     * @Description: 找到已分配给此用户的角色list
    */
    @Override
    public List<Role> getAssignedRoleList(Integer adminId) {
        return roleMapper.selectAssignedRoleList(adminId);
    }


    /**
     * @Author:Stone
       * @Param adminId:
       * @return: java.util.List<com.crowd.entity.Role>
     * @Date: 2020/5/25 14:19
     * @Description:找到未分配给此用户的角色list
    */
    @Override
    public List<Role> getUnAssignedRoleList(Integer adminId) {
        return roleMapper.selectUnAssignedRoleList(adminId);
    }

    /**
     * @Author:Stone
       * @Param adminId:
     * @Param assignedRoleList:
       * @return: void
     * @Date: 2020/5/26 14:57
     * @Description:给用户修改角色权限，有可能新增，有可能删除
    */
    @Override
    public void saveAdminRoleRelationship(Integer adminId, List<Integer> assignedRoleList) {
        // 1.删除旧权限
        roleMapper.deleteOldAdminRoleRelationship(adminId);
        // 2.添加新权限
        if(assignedRoleList!=null && assignedRoleList.size()>0){
            roleMapper.insertNewAdminRoleRelationship(adminId,assignedRoleList);
        }

    }
}
